import { useState, useCallback } from 'react';
import axios from 'axios';

function useAxios() {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(null);
  const [response, setResponse] = useState(null);

  const sendRequest = useCallback(async (url, method = 'GET', payload = null) => {
    try {
      setLoading(true);
      setError(null);
      setResponse(null);

      const result = await axios({
        method: method,
        url: url,
        data: payload,
      });

      setResponse(result.data);
      console.log(result.data,'参数：',payload)
    } catch (err) {
      setError(err);
      console.log(err)
    } finally {
      setLoading(false);
    }
  }, []);

  return { sendRequest, loading, error, response };
}

export default useAxios;